<?php
namespace app\admin\controller;
use app\index\model\Users;
use app\index\model\Order;
use think\Controller;
use think\Db;
class Result extends Common
{
	//开奖记录
	public function index(){
		//显示全部数组
		$all = input('get.all');
		//开奖记录信息
		$data = model('Result')->index();
		//下一期订单明细
		$next = model('Result')->next();
        $this->assign('all',$all);
        $this->assign('data',$data);
        $this->assign('next',$next);
        return $this->fetch();
	}

	//查看订单明细
	public function see(){
		//买涨人数
		$qishu = input('param.qishu');
		$status = input('param.status');
		$up = db('order')
		->group('uid')
		->where('qishu',$qishu)
		->where('updown',1)
        ->count();

        //买涨金额
        $ups = db('order')
        ->field('SUM(money) as moneys')
		->where('qishu',$qishu)
		->where('updown',1)
        ->select();
        
        //买跌人数
		$down = db('order')
		->group('uid')
		->where('qishu',$qishu)
		->where('updown',0)
        ->count();
        //买跌金额
        $downs = db('order')
        ->field('SUM(money) as moneys')
		->where('qishu',$qishu)
		->where('updown',0)
        ->select();
        $set = db('set')->field('allodds')->select();
        $allodds = $set[0]['allodds'];
        $this->assign('up',$up);
        $this->assign('down',$down);
        $this->assign('ups',$ups);
        $this->assign('downs',$downs);
        $this->assign('allodds',$allodds);
        $this->assign('status',$status);
        return $this->fetch();
	}

	//删除单条记录(删除数据)
	public function del(){
		$id = input('param.id');
		$del = db('result')->where('id',$id)->delete();
		if($del > 0){
			$content = '删除开奖记录';
        	admin_log($content); 
			$this->success('删除成功','index','',1);
		}else{
			$this->error('删除失败','','',1);
		}
	}

	//批量删除(批量删除数据)
	public function deleteall(){
		if(request() -> isAjax()){
			$id=$_POST['id'];
			if(db('result')->delete($id)){
				$content = '删除开奖记录';
        	    admin_log($content); 
				echo json_encode(1);
			}else{
				echo json_encode(2);
			}

		}
	}

    //手动开奖
	public function kai(){
        //赔率的设置
        $set = db('set')->field('allodds')->select();
        $allodds = $set[0]['allodds'];
        $data['qishu'] = input('param.qishu');
        $data['updown'] = input('param.status');
        $datas['qishu'] = input('param.qishu');
        $datas['updown'] = array('neq',$data['updown']);
        if(!isset($data['updown'])){
            $this->error("该期数未开,不能进行开奖");
        }
        //用户中奖状态更改
        $alls = db('order')
             ->field('oid,uid,money,status')
             ->where($data)
             ->select(); 
        foreach($alls as $key => $value){        
            if($value['status'] == 3){
                 $win[$key]['oid'] = $value['oid'];
                 $win[$key]['uid'] = $value['uid'];
                 $win[$key]['status'] = 1;
                 $win[$key]['winmoney'] = $value['money']*($allodds);
                 $win[$key]['wintime'] = date('Y-m-d H:i:s');
            }
        } 
        //用户中奖更改
        if(!empty($win)){
            $order = new Order;
            $msg['zhong'] = $order->saveAll($win);
            $all = db('order')
                       ->field('uid,SUM(money) as money')
                       ->group('uid')
                       ->where($data)
                       ->select(); 
                   foreach($all as $key => $value){
                    $user = db('users')->field('uid,money')->where('uid',$value['uid'])->select();
                         foreach ($user as $k => $v) {
                               $all[$key]['money']= $v['money']+$allodds*$value['money'];
                         }
                    } 
                    $users = new Users;
                    $msg['users'] = $users->saveAll($all); 
        }
        //用户未中奖状态更改
        $all1 =  db('order')
             ->field('oid,uid,money,status')
             ->where($datas)
             ->select();      
        foreach($all1 as $key => $value){
            if($value['status'] == 3){
                $wins[$key]['oid'] = $value['oid'];
                $wins[$key]['uid'] = $value['uid'];
                $wins[$key]['status'] = 0;
                $wins[$key]['winmoney'] = 0;
                $wins[$key]['wintime'] = date('Y-m-d H:i:s');
           }
        }
        if(!empty($wins)){
            $order = new Order;
            $msg['wei'] = $order->saveAll($wins); 
        }
        if(!$alls && !$all1){
            $open['open'] = 1;
            $qishu = input('param.qishu');
            $qi = db('result')->where('qishu',$qishu)->update($open);
        }
        if(isset($msg)){
            $open['open'] = 1;
            $qishu = input('param.qishu');
            $qi = db('result')->where('qishu',$qishu)->update($open);    
        }
        if(isset($msg)){
            $this->success('开奖成功','index','',1);
        }else{
            $this->error('已开奖','index','',1);
        }
     }

      
    // public function del(){
    //     echo 222;
    // }
}